package main

import (
	"context"
	"fmt"
	"github.com/spf13/viper"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"go.uber.org/zap"
	"google.golang.org/grpc"
	"log"
	blobpb "serve/blob/api/gen/v1"
	carpb "serve/car/api/gen/v1"
	rentalpb "serve/rental/api/gen/v1"
	profileService "serve/rental/profile"
	"serve/rental/profile/client/orc"
	dao2 "serve/rental/profile/dao"
	"serve/rental/trip"
	"serve/rental/trip/client/car"
	"serve/rental/trip/client/poi"
	"serve/rental/trip/client/profile"
	"serve/rental/trip/dao"
	"serve/settings"
	"serve/shared/server"
	"time"
)

func init() {
	err := settings.Init()
	if err != nil {
		log.Fatalf("settings.Init() %v", err)
	}
}

func main() {
	logger, err := server.NewZapLogger()
	if err != nil {
		log.Fatalf("cannot create logger err %v", err)
	}

	c := context.Background() // 建立一个content
	//MongoClient, err := mongo.Connect(c, // 创建mongo连接
	//	options.Client().ApplyURI(fmt.Sprintf("mongodb://%s:%s@%s:%d",
	//		viper.GetString("mongodb.user"),
	//		viper.GetString("mongodb.password"),
	//		viper.GetString("mongodb.host"),
	//		viper.GetInt("mongodb.port"),
	//	)))

	MongoClient, err := mongo.Connect(c, // 创建mongo连接
		options.Client().ApplyURI(fmt.Sprintf("mongodb://%s:%s@%s:%d",
			viper.GetString("mongodb.user"),
			viper.GetString("mongodb.password"),
			viper.GetString("mongodb.host"),
			viper.GetInt("mongodb.port"),
		)))

	if err != nil {
		logger.Fatal("mongo.Connect err", zap.Error(err))
	}

	blobConn, err := grpc.Dial(fmt.Sprintf("%s:%d",
		viper.GetString("blob.srv.host"),
		viper.GetInt("blob.srv.port"),
	), grpc.WithInsecure())

	if err != nil {
		logger.Fatal("blob.Connect err", zap.Error(err))
	}

	newOrc, err := orc.NewOrc(
		viper.GetString("ocr.secID"),
		viper.GetString("ocr.secKey"),
		viper.GetString("ocr.addr"),
	)
	if err != nil {
		logger.Fatal("orc.Connect err", zap.Error(err))
	}

	pfileService := &profileService.Service{
		Mongo:             dao2.NewMongo(MongoClient.Database(viper.GetString("mongodb.dbname"))),
		Logger:            logger,
		BlobClient:        blobpb.NewBlobServiceClient(blobConn),
		PhotoUploadExpire: 60 * time.Second,
		PhotoGetExpire:    60 * time.Second,
		OCrManager:        newOrc,
	}

	carConn, err := grpc.Dial(fmt.Sprintf("%s:%d",
		viper.GetString("car.srv.host"),
		viper.GetInt("car.srv.port"),
	), grpc.WithInsecure())

	if err != nil {
		logger.Fatal("Connect car err ", zap.Error(err))
	}

	err = server.RunGRPCServer(&server.GrpcConfig{
		Name:              "rental",
		Addr:              viper.GetInt("rental.srv.port"),
		AuthPublicKeyFile: "./public.key",
		RegisterFunc: func(server *grpc.Server) {
			rentalpb.RegisterTripServiceServer(server, &trip.Service{ // 注册trip的Server
				Logger:     logger, // 将日志传入Server
				Mongo:      dao.NewMongo(MongoClient.Database(viper.GetString("mongodb.dbname"))),
				CarManager: &car.Manager{CarService: carpb.NewCarServiceClient(carConn)},
				ProfileManager: &profile.Manger{
					Fetcher: pfileService,
				},
				POIManager: &poi.Manager{},
			})

			rentalpb.RegisterProfileServiceServer(server, pfileService)
		},
		Logger: logger,
	})

	if err != nil {
		logger.Fatal(" server.RunGRPCServer()", zap.Error(err))
	}
}
